package com.cn.lotary.util;

import java.util.List;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Jason
 */
public class Mailer {

    /**
     * @see SystemConfig
     *
     * @param from
     * @param to
     * @param subject
     * @param body
     */
    public static void sendMail(String from, String to, String subject, String body) {
        if (!SystemConfig.send_exception_email) {
            LoggerUtils.info(subject + "\n" + body);
            return;
        }
        System.setProperty("mail.mime.charset", "UTF-8");
        if (from == null) {
            from = SystemConfig.EMAIL_FROM;
        }
        if (to == null) {
            to = SystemConfig.EMAIL_TO;
        }
        if (!ObjectUtil.eamilValidation(to) || !ObjectUtil.eamilValidation(from)) {
            LoggerUtils.warn("[sendMail] email address incorrent !");
            return;
        }


        // TODO code application logic here
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", SystemConfig.MAIL_TRANSPORT_PROTOCOL);
        properties.put("mail.smtp.host", SystemConfig.MAIL_SMTP_HOST);
        properties.put("mail.smtp.port", SystemConfig.MAIL_SMTP_PORT);
        Session session = Session.getDefaultInstance(properties, null);

        MimeMessage message = new MimeMessage(session);
        try {

            message.setFrom(new InternetAddress(from));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);
            message.setContent(body, "text/html;charset=utf-8");
            //message.setContent(body);
            // message.setText(body);
            Transport.send(message);
        } catch (AddressException e) {
            LoggerUtils.error(e);
        } catch (MessagingException e) {
            LoggerUtils.error(e);
        }
    }

    /**
     * 发送给多个地址
     *
     * @param from
     * @param to 多个邮件 List
     * @param subject
     * @param body
     */
    public static void sendMails(String from, List<String> to, String subject, String body) {
        if (!SystemConfig.send_exception_email) {
            LoggerUtils.info(subject + "\n" + body);
            return;
        }
        if (to.isEmpty()) {
            LoggerUtils.warn("sendMails param to is empty");
            return;
        }
        System.setProperty("mail.mime.charset", "UTF-8");
        if (from == null) {
            from = SystemConfig.EMAIL_FROM;
        }
        if (!ObjectUtil.eamilValidation(from)) {
            LoggerUtils.warn("[sendMails] param from is incorrent email address");
            return;
        }

        // TODO code application logic here
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", SystemConfig.MAIL_TRANSPORT_PROTOCOL);
        properties.put("mail.smtp.host", SystemConfig.MAIL_SMTP_HOST);
        properties.put("mail.smtp.port", SystemConfig.MAIL_SMTP_PORT);
        Session session = Session.getDefaultInstance(properties, null);

        MimeMessage message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(from));

            InternetAddress[] addresses = new InternetAddress[to.size()];
            StringBuilder eamil = new StringBuilder();
            for (int i = 0; i < addresses.length; i++) {
                if (ObjectUtil.eamilValidation(to.get(i))) {
                    addresses[i] = new InternetAddress(to.get(i));
                } else {
                    eamil.append("[").append(to.get(i)).append("]");
                }
            }
            if (eamil.length() > 0) {
                LoggerUtils.warn(eamil.toString() + " is not email address");
            }

            message.setRecipients(Message.RecipientType.TO, addresses);

            message.setSubject(subject);
            message.setContent(body, "text/html;charset=utf-8");

            //message.setText(body);
            Transport.send(message);
        } catch (AddressException e) {
            LoggerUtils.error(e);
        } catch (MessagingException e) {
            LoggerUtils.error(e);
        }
    }

    /**
     * @deprecated 发送给多个地址
     *
     * @param from
     * @param to 多个邮件 数组
     * @param subject
     * @param body
     */
    public static void sendMails(String from, String[] to, String subject, String body) {
        if (!SystemConfig.send_exception_email) {
            LoggerUtils.info(subject + "\n" + body);
            return;
        }
        System.setProperty("mail.mime.charset", "UTF-8");
        if (from == null) {
            from = SystemConfig.EMAIL_FROM;
        }

        // TODO code application logic here
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", SystemConfig.MAIL_TRANSPORT_PROTOCOL);
        properties.put("mail.smtp.host", SystemConfig.MAIL_SMTP_HOST);
        properties.put("mail.smtp.port", SystemConfig.MAIL_SMTP_PORT);
        Session session = Session.getDefaultInstance(properties, null);

        MimeMessage message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(from));

            InternetAddress[] addresses = new InternetAddress[to.length];

            for (int i = 0; i < addresses.length; i++) {
                addresses[i] = new InternetAddress(to[i]);
            }




            message.setRecipients(Message.RecipientType.TO, addresses);

            message.setSubject(subject);
            message.setContent(body, "text/html;charset=utf-8");
            //message.setText(body);
            Transport.send(message);
        } catch (AddressException e) {
            LoggerUtils.error(e);
        } catch (MessagingException e) {
            LoggerUtils.error(e);
        }
    }
}
